package test.com.jse;
import java.io.OutputStream;
import java.lang.String;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/*****7

 * @jjw QQ223783751 * 网上百度一大堆总结得方法就是用runtime 但是exec后用in.read 好像线程被挂起了
 * 转载请注明出处
 * 后面我想到c肯定可以操作得去百度发现了管道通信代码
 * 通过阅读代码发现 错误就错误在网上说得 如果没把三个流（getInputStream getOutputStream getErrorStream ）处理干净会挂起 线程
 * 从我得代码中多次调试我发现并不是这个问题，我想到得就是 in.read 会一直在读东西直到有内容被读到 启发来自     Scanner sc = new Scanner(System.in);
 *经过多次探索确定了这个问题   那么问题就出在输出流，于是我尝试了，还是不行，最后通过网上得c++ 管道通信看到了原因就是输出流只输出没做回车动作
 * 那么到此问题解决    在windos系统中 \r\n 回车    不同系统得换行符号参考   CRLF、CR、LF
 * */
public class DemoMian {

    public static void main(String[] args) throws IOException, InterruptedException {
        String [] params={"cmd","node"};
        Process process=  runCmd(params);
        CmdInThread cmdInThread=new CmdInThread("cmdInThread",process);
        cmdInThread.start();

        CmdOutThread cmdOutThread=new CmdOutThread("CmdOutThread",cmdInThread,process);
        cmdOutThread.start();

    }

    public static  Process  runCmd(String [] cmd) throws IOException, InterruptedException {
         Runtime runtime=Runtime.getRuntime();
         Process pro=Runtime.getRuntime().exec(cmd);
         return  pro;
    }

}

class CmdInThread extends Thread{
    private   Process process;
    private  int num=1;
    public CmdInThread(String name,Process process) {
        super(name);
        this.process=process;
    }

    @Override
    public synchronized void run() {
        while (true){
            try {
                InputStream in =process.getInputStream();
//                OutputStream outputStream=process.getOutputStream();
                //读入数据
                byte[] data = new byte[1024];
                int readlen = in.read(data);
                System.out.println(  new String(data,"gb2312"));
//                System.out.println(process.isAlive());
//                String  sss="hostName \r\n";
//                outputStream.write(sss.getBytes());
//                outputStream.flush();
//                this.wait();
            }catch (IOException i){
                System.out.println(i.getMessage());
            }
        }

    }
}


class CmdOutThread extends Thread{
    private CmdInThread cmdInThread;
    private   Process process;

    public CmdOutThread(String name, CmdInThread cmdInThread, Process process) {
        super(name);
        this.cmdInThread = cmdInThread;
        this.process = process;
    }

    @Override
    public void run() {
        while (true){
            Scanner sc = new Scanner(System.in);
            String code=sc.nextLine() +" \r\n";
            OutputStream out=process.getOutputStream();
            try {
                out.write(code.getBytes());
                out.flush();
//                synchronized (cmdInThread){
//                    cmdInThread.notify();
//                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(code);
        }
    }
}